package com.btex.app.treaty.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.btex.app.Config;
import com.btex.app.R;
import com.btex.app.entity.YuCheLeverInfo;
import com.btex.app.tools.Tools;
import com.btex.app.tools.Util;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhouyou.view.seekbar.SignSeekBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.btex.app.treaty.dialog.NoLeverDialog.getIndex;

/**
 * @author zy zhangyi <zhangyi, 1104745049@QQ.com
 * @version v1.0
 * @project study1
 * @Description
 * @encoding UTF-8
 * @date 2018/10/15
 * @time 下午6:05
 * @修改记录 <pre>
 * 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 * </pre>
 */

public class LeverDialog extends Dialog {


    @BindView(R.id.lever_dialog_progress)
    ProgressBar mLeverDialogProgress;
    @BindView(R.id.lever_dialog_one)
    LinearLayout mLeverDialogOne;
    @BindView(R.id.lever_dialog_two)
    LinearLayout mLeverDialogTwo;
    @BindView(R.id.lever_dialog_three)
    LinearLayout mLeverDialogThree;
    @BindView(R.id.lever_dialog_four)
    LinearLayout mLeverDialogFour;
    @BindView(R.id.lever_dialog_fine)
    LinearLayout mLeverDialogFine;
    @BindView(R.id.lever_dialog_six)
    LinearLayout mLeverDialogSix;
    @BindView(R.id.lever_dialog_seven)
    LinearLayout mLeverDialogSeven;
    @BindView(R.id.lever_dialog_eight)
    LinearLayout mLeverDialogEight;
    @BindView(R.id.lever_dialog_one_imag)
    ImageView mLeverDialogOneImag;
    @BindView(R.id.lever_dialog_two_imag)
    ImageView mLeverDialogTwoImag;
    @BindView(R.id.lever_dialog_three_imag)
    ImageView mLeverDialogThreeImag;
    @BindView(R.id.lever_dialog_four_imag)
    ImageView mLeverDialogFourImag;
    @BindView(R.id.lever_dialog_fine_imag)
    ImageView mLeverDialogFineImag;
    @BindView(R.id.lever_dialog_six_imag)
    ImageView mLeverDialogSixImag;
    @BindView(R.id.lever_dialog_seven_imag)
    ImageView mLeverDialogSevenImag;
    @BindView(R.id.lever_dialog_eight_imag)
    ImageView mLeverDialogEightImag;
    @BindView(R.id.lever_dialog_lever_tv_1)
    TextView mLeverDialogLeverTv1;
    @BindView(R.id.lever_dialog_lever_tv_2)
    TextView mLeverDialogLeverTv2;
    @BindView(R.id.lever_dialog_lever_tv_3)
    TextView mLeverDialogLeverTv3;
    @BindView(R.id.lever_dialog_lever_tv_4)
    TextView mLeverDialogLeverTv4;
    @BindView(R.id.lever_dialog_lever_tv_5)
    TextView mLeverDialogLeverTv5;
    @BindView(R.id.lever_dialog_lever_tv_6)
    TextView mLeverDialogLeverTv6;
    @BindView(R.id.lever_dialog_lever_tv_7)
    TextView mLeverDialogLeverTv7;
    @BindView(R.id.lever_dialog_lever_tv_8)
    TextView mLeverDialogLeverTv8;
    @BindView(R.id.lever_dialog_quan_imag)
    ImageView mLeverDialogQuanImag;
    @BindView(R.id.lever_dialog_fine_tv_diss)
    TextView mLeverDialogFineTvDiss;
    @BindView(R.id.lever_dialog_fine_tv_sure)
    TextView mLeverDialogFineTvSure;
    @BindView(R.id.lever_dialog_fine_tv_baozhengjing)
    TextView mLeverDialogFineTvBaozhengjing;
    @BindView(R.id.lever_dialog_fine_tv_qiangpingjia)
    TextView mLeverDialogFineTvQiangpingjia;

    @BindView(R.id.lever_dialog_tv_quancang)
    TextView lever_dialog_tv_quancang;
    @NonNull
    private final Context mContext;
    @BindView(R.id.jiazai)
    ImageView mJiazai;
    @BindView(R.id.lever_dialog_fine_tv_sure_lin)
    LinearLayout mLeverDialogFineTvSureLin;
    @BindView(R.id.dialog_lever_zucang)
    Button mDialogLeverZucang;
    @BindView(R.id.dialog_lever_quancang)
    Button mDialogLeverQuancang;
    @BindView(R.id.jiazai_is_quancang)
    ImageView mJiazaiIsQuancang;
    @BindView(R.id.demo_5_seek_bar_2)
    SignSeekBar mDemo5SeekBar2;
    private ArrayList<String> mStrings;
    private String mContract_id;
    private String mGanggan;
    private String mMIs_cross;
    private String mAllow_cross;
    private String mPriceNumber = "6";
    private String mMPrice_float_num = "4";
    private int mType;
    private String mTiaoganggan;
    public String isQuan = "1";


    public LeverDialog(@NonNull Context context, ArrayList<String> strings, String leverage, String ganggan, String mIs_cross, String allow_cross, String priceNumber, String mPrice_float_num) {
        super(context, R.style.coupon_style);
        mContext = context;
        mStrings = strings;
        mContract_id = leverage;
        mGanggan = ganggan;
        mMIs_cross = mIs_cross;
        mAllow_cross = allow_cross;
        mPriceNumber = priceNumber;
        mMPrice_float_num = mPrice_float_num;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lever_dialog);
        ButterKnife.bind(this);
        mTiaoganggan = mGanggan;
        Glide.with(getContext()).load(R.drawable.jiazai).into(mJiazai);
        Glide.with(getContext()).load(R.drawable.jiazai).into(mJiazaiIsQuancang);


        initViews();

        //获取屏幕宽高
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        //屏幕宽
        int widthPixels = displayMetrics.widthPixels;
        //屏幕高
        int heightPixels = displayMetrics.heightPixels;
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        //设置给attributes
        attributes.width = (int) (widthPixels * 0.95);
        attributes.height = (int) (heightPixels * 0.5);
        //让Dialog位置居中
        attributes.gravity = Gravity.CENTER;
        getWindow().setAttributes(attributes);
        //点击空白处不dismiss
        setCanceledOnTouchOutside(true);


        List<String> old = new ArrayList<>();
        for (int i = 0; i < mStrings.size(); i++) {
            old.add(mStrings.get(i) + "X");
        }
        String[] lever = old.toArray(new String[old.size()]);
        int length = lever.length;
        Log.i("weuwiheiwuehiweh", length + "");
        String[] newlever = mStrings.toArray(new String[mStrings.size()]);
        int index = getIndex(newlever, mGanggan);
//        double i = Double.valueOf(String.valueOf(mStrings.size())) / (Double.valueOf(String.valueOf(mStrings.size() - 1)));
//        double v1 = i * Double.valueOf(index);
        mDemo5SeekBar2.getConfigBuilder()
                .max(lever.length - 1)
                .progress(index)
                .bottomSidesLabels(lever)
                .sectionCount(lever.length - 1)
                .thumbColor(ContextCompat.getColor(getContext(), R.color.otc_set))
                .sectionTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimary))
                .sectionTextSize(11)
                .sectionTextPosition(SignSeekBar.TextPosition.BELOW_SECTION_MARK)
                .build();
        mDemo5SeekBar2.setOnProgressChangedListener(new SignSeekBar.OnProgressChangedListener() {
            @Override
            public void onProgressChanged(SignSeekBar signSeekBar, int progress, float progressFloat, boolean fromUser) {

            }

            @Override
            public void getProgressOnActionUp(SignSeekBar signSeekBar, int progress, float progressFloat) {

            }

            @Override
            public void getProgressOnFinally(SignSeekBar signSeekBar, int progress, float progressFloat, boolean fromUser) {

                Log.i("weijfowijefoiewewewjef", progress + "");
//                if (progress == 0) {
//                    mTiaoganggan = mStrings.get(progress);
//                } else {
//                    mTiaoganggan = mStrings.get(progress - 1);
//                }
                mTiaoganggan = mStrings.get(progress);

                getCost();


            }
        });


    }

    private void initViews() {


        if (mAllow_cross.equals("-1")) {

            mLeverDialogQuanImag.setClickable(false);
            lever_dialog_tv_quancang.setVisibility(View.INVISIBLE);
        }

        mType = 10000;


        String[] split = mGanggan.split("\\.");
        String s1 = split[0];
        Log.i("wleidjowiejoiwe", s1);


        for (int i = 0; i < mStrings.size(); i++) {

            String s = mStrings.get(i);

            if (s1.equals(s)) {
                mType = i;
            }

        }

        if (mMIs_cross.equals("1")) {
            mDialogLeverQuancang.setSelected(true);

        } else {
            mDialogLeverZucang.setSelected(true);
        }


        if (mMIs_cross.equals("1")) {
            mLeverDialogProgress.setProgress(0);
            SetStat();
        } else {
            setProgress(mType);
        }


        mLeverDialogLeverTv1.setText(mStrings.get(0) + "x");
        mLeverDialogLeverTv2.setText(mStrings.get(1) + "x");
        mLeverDialogLeverTv3.setText(mStrings.get(2) + "x");
        mLeverDialogLeverTv4.setText(mStrings.get(3) + "x");
        mLeverDialogLeverTv5.setText(mStrings.get(4) + "x");
        mLeverDialogLeverTv6.setText(mStrings.get(5) + "x");
        mLeverDialogLeverTv7.setText(mStrings.get(6) + "x");
        mLeverDialogLeverTv8.setText(mStrings.get(7) + "x");

    }

    private void setProgress(int type) {

        if (mType == 10000) {
            SetStat();
            mLeverDialogProgress.setProgress(0);

        }
        if (mType == 0) {

            mLeverDialogProgress.setProgress(9);
            SetStat();
            mLeverDialogOneImag.setBackgroundResource(R.mipmap.qihuo_y_lever);

        } else if (mType == 1) {

            mLeverDialogProgress.setProgress(25);
            SetStat();
            mLeverDialogTwoImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogOneImag.setBackgroundResource(R.mipmap.qihuo_y_lever);

        } else if (mType == 2) {

            mLeverDialogProgress.setProgress(37);
            SetStat();
            mLeverDialogTwoImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogOneImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogThreeImag.setBackgroundResource(R.mipmap.qihuo_y_lever);


        } else if (mType == 3) {

            mLeverDialogProgress.setProgress(50);
            SetStat();

            mLeverDialogTwoImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogOneImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogThreeImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogFourImag.setBackgroundResource(R.mipmap.qihuo_y_lever);

        } else if (mType == 4) {

            mLeverDialogProgress.setProgress(62);
            SetStat();
            mLeverDialogTwoImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogOneImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogThreeImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogFourImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogFineImag.setBackgroundResource(R.mipmap.qihuo_y_lever);

        } else if (mType == 5) {

            mLeverDialogProgress.setProgress(76);
            SetStat();
            mLeverDialogTwoImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogOneImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogThreeImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogFourImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogFineImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogSixImag.setBackgroundResource(R.mipmap.qihuo_y_lever);

        } else if (mType == 6) {

            mLeverDialogProgress.setProgress(88);
            SetStat();
            mLeverDialogTwoImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogOneImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogThreeImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogFourImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogFineImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogSixImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogSevenImag.setBackgroundResource(R.mipmap.qihuo_y_lever);

        } else if (mType == 7) {

            mLeverDialogProgress.setProgress(100);
            SetStat();
            mLeverDialogTwoImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogOneImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogThreeImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogFourImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogFineImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogSixImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogSevenImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
            mLeverDialogEightImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
        }
    }


    @OnClick({R.id.lever_dialog_progress, R.id.lever_dialog_one, R.id.lever_dialog_two, R.id.lever_dialog_three, R.id.lever_dialog_four, R.id.lever_dialog_fine, R.id.lever_dialog_six, R.id.lever_dialog_seven, R.id.lever_dialog_eight, R.id.lever_dialog_quan_imag, R.id.lever_dialog_fine_tv_diss, R.id.lever_dialog_fine_tv_sure_lin, R.id.dialog_lever_zucang, R.id.dialog_lever_quancang, R.id.dialog_lever_quancang_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.lever_dialog_progress:

                break;
            case R.id.lever_dialog_one:
                mMIs_cross = "-1";
                mTiaoganggan = mStrings.get(0);
                SetStat();
                mLeverDialogProgress.setProgress(13);
                mLeverDialogOneImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                getCost();
                break;
            case R.id.lever_dialog_two:
                mMIs_cross = "-1";
                mTiaoganggan = mStrings.get(1);
                SetStat();

                mLeverDialogProgress.setProgress(26);
                mLeverDialogTwoImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogOneImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                getCost();
                break;
            case R.id.lever_dialog_three:
                mMIs_cross = "-1";
                mTiaoganggan = mStrings.get(2);
                SetStat();
                mLeverDialogProgress.setProgress(37);
                mLeverDialogTwoImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogOneImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogThreeImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                getCost();
                break;
            case R.id.lever_dialog_four:
                mMIs_cross = "-1";
                mTiaoganggan = mStrings.get(3);
                SetStat();
                mLeverDialogProgress.setProgress(50);
                mLeverDialogTwoImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogOneImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogThreeImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogFourImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                getCost();
                break;
            case R.id.lever_dialog_fine:
                mMIs_cross = "-1";
                mTiaoganggan = mStrings.get(4);
                SetStat();
                mLeverDialogProgress.setProgress(62);
                mLeverDialogTwoImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogOneImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogThreeImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogFourImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogFineImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                getCost();
                break;
            case R.id.lever_dialog_six:
                mMIs_cross = "-1";
                mTiaoganggan = mStrings.get(5);
                SetStat();
                mLeverDialogProgress.setProgress(76);
                mLeverDialogTwoImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogOneImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogThreeImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogFourImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogFineImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogSixImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                getCost();
                break;
            case R.id.lever_dialog_seven:
                mMIs_cross = "-1";
                mTiaoganggan = mStrings.get(6);

                SetStat();
                mLeverDialogProgress.setProgress(88);
                mLeverDialogTwoImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogOneImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogThreeImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogFourImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogFineImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogSixImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogSevenImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                getCost();
                break;
            case R.id.lever_dialog_eight:
                mMIs_cross = "-1";
                mTiaoganggan = mStrings.get(7);

                SetStat();
                mLeverDialogProgress.setProgress(100);
                mLeverDialogTwoImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogOneImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogThreeImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogFourImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogFineImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogSixImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogSevenImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                mLeverDialogEightImag.setBackgroundResource(R.mipmap.qihuo_y_lever);
                getCost();
                break;

            case R.id.lever_dialog_quan_imag:
                mLeverDialogProgress.setProgress(0);
                if (mAllow_cross.equals("-1")) {
                    mMIs_cross = "-1";
                } else {
                    mMIs_cross = "1";
                }

                SetStat();
                getCost();

                break;

            case R.id.lever_dialog_fine_tv_diss:

                dismiss();
                break;

            case R.id.lever_dialog_fine_tv_sure_lin:
                mLeverDialogFineTvSureLin.setClickable(false);

                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        //                耗时操作
                        Message message = new Message();
                        message.what = 1;
                        handlers.sendMessage(message);
                    }
                }).start();


                break;

            case R.id.dialog_lever_zucang:
                isQuan = "-1";
                mMIs_cross = "-1";
                setState();
                mDialogLeverZucang.setSelected(true);
                break;

            case R.id.dialog_lever_quancang:
                isQuan = "1";
                mMIs_cross = "1";
                setState();
                mDialogLeverQuancang.setSelected(true);
                break;

            case R.id.dialog_lever_quancang_sure:
                setStatePost("");
                break;
        }
    }


    public void SetStat() {
        mLeverDialogOneImag.setBackgroundResource(R.mipmap.qihuo_no_lever);
        mLeverDialogTwoImag.setBackgroundResource(R.mipmap.qihuo_no_lever);
        mLeverDialogThreeImag.setBackgroundResource(R.mipmap.qihuo_no_lever);
        mLeverDialogFourImag.setBackgroundResource(R.mipmap.qihuo_no_lever);
        mLeverDialogFineImag.setBackgroundResource(R.mipmap.qihuo_no_lever);
        mLeverDialogSixImag.setBackgroundResource(R.mipmap.qihuo_no_lever);
        mLeverDialogSevenImag.setBackgroundResource(R.mipmap.qihuo_no_lever);
        mLeverDialogEightImag.setBackgroundResource(R.mipmap.qihuo_no_lever);


    }

//    获取调整杠杆的预测值

    private void getCost() {

        Log.i("weoifjowijfoiwe", mContract_id + "==========" + mTiaoganggan + "is_cross=========" + mMIs_cross);
        int num = (int) ((Math.random() * 9 + 1) * 100000);
        OkGo.<String>post(Config.PRE_CHANGE_LEVERAGE + "?rnd=" + String.valueOf(num)).params("position_id", mContract_id).params("leverage", mTiaoganggan).params("is_cross", mMIs_cross).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String result = response.body().toString();
                Log.i("nchwetwekwueh", result);

                try {
                    JSONObject jsonObject = new JSONObject(result);

                    if (jsonObject.getString("code").equals("1")) {
                        Gson gson = new Gson();
                        YuCheLeverInfo yuCheLeverInfo = gson.fromJson(result, YuCheLeverInfo.class);

                        String New_margin = Tools.round(Double.valueOf(yuCheLeverInfo.getData().getNew_margin()), Integer.valueOf(mPriceNumber)).toString().trim();
                        String New_liq_price = Tools.round(Double.valueOf(yuCheLeverInfo.getData().getNew_liq_price()), Integer.valueOf(mMPrice_float_num)).toString().trim();
                        mLeverDialogFineTvQiangpingjia.setText(New_liq_price);
                        mLeverDialogFineTvBaozhengjing.setText(New_margin);

                    } else {
                        Util.showSnachBarDialog(mLeverDialogProgress, mContext.getResources().getColor(R.color.red), jsonObject.getString("info"));
                    }

                } catch (JSONException e) {


                }
            }
        });


//        SharedPreferencesUtil sharedPreferences = SharedPreferencesUtil.getSharedPreferences(getContext());
//        PreferencesCookieStore cookieStore = CookieTool.getCookieStore(getContext(), sharedPreferences);
//        RequestParams params = new RequestParams();
//        params.addBodyParameter("position_id", mContract_id);
//        params.addBodyParameter("leverage", mTiaoganggan);
//        params.addBodyParameter("is_cross", mMIs_cross);
//        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.POST, Config.PRE_CHANGE_LEVERAGE + "?rnd=" + String.valueOf(num), params, new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//
//                Log.i("nchwetwekwueh", responseInfo.result);
//
//                try {
//                    JSONObject jsonObject = new JSONObject(responseInfo.result);
//
//                    if (jsonObject.getString("code").equals("1")) {
//                        Gson gson = new Gson();
//                        YuCheLeverInfo yuCheLeverInfo = gson.fromJson(responseInfo.result, YuCheLeverInfo.class);
//
//                        String New_margin = Tools.round(Double.valueOf(yuCheLeverInfo.getData().getNew_margin()), Integer.valueOf(mPriceNumber)).toString().trim();
//                        String New_liq_price = Tools.round(Double.valueOf(yuCheLeverInfo.getData().getNew_liq_price()), Integer.valueOf(mMPrice_float_num)).toString().trim();
//                        mLeverDialogFineTvQiangpingjia.setText(New_liq_price);
//                        mLeverDialogFineTvBaozhengjing.setText(New_margin);
//
//
//                    } else {
//
//                        Util.showSnachBarDialog(mLeverDialogProgress, mContext.getResources().getColor(R.color.red), jsonObject.getString("info"));
//
//                    }
//
//                } catch (JSONException e) {
//
//
//                }
//
//            }
//
//            @Override
//            public void onFailure(HttpException error, String msg) {
//
//
//            }
//
//        });
    }


    //    调整杠杆
    private void getCosts() {
        int num = (int) ((Math.random() * 9 + 1) * 100000);
        mJiazai.setVisibility(View.VISIBLE);
        OkGo.<String>post(Config.DO_CHANGE_LEVERAGE + "?rnd=" + String.valueOf(num)).params("position_id", mContract_id).params("leverage", mTiaoganggan).params("is_cross", mMIs_cross).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String result = response.body().toString();
                mLeverDialogFineTvSureLin.setClickable(true);
                mJiazai.setVisibility(View.GONE);
                Log.i("nchwetwekwueh", result);

                try {
                    JSONObject jsonObject = new JSONObject(result);

                    if (jsonObject.getString("code").equals("1")) {
                        dismiss();
                        Util.showSnachBarDialog(mLeverDialogProgress, mContext.getResources().getColor(R.color.bg_green), "操作成功");

                    } else {
                        Util.showSnachBarDialog(mLeverDialogProgress, mContext.getResources().getColor(R.color.red), jsonObject.getString("info"));
                    }

                } catch (JSONException e) {


                }
            }
        });


//        SharedPreferencesUtil sharedPreferences = SharedPreferencesUtil.getSharedPreferences(getContext());
//        PreferencesCookieStore cookieStore = CookieTool.getCookieStore(getContext(), sharedPreferences);
//        RequestParams params = new RequestParams();
//        params.addBodyParameter("position_id", mContract_id);
//        params.addBodyParameter("leverage", mTiaoganggan);
//        params.addBodyParameter("is_cross", mMIs_cross);
//
//        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.POST, Config.DO_CHANGE_LEVERAGE + "?rnd=" + String.valueOf(num), params, new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//                mLeverDialogFineTvSureLin.setClickable(true);
//                mJiazai.setVisibility(View.GONE);
//                Log.i("nchwetwekwueh", responseInfo.result);
//
//                try {
//                    JSONObject jsonObject = new JSONObject(responseInfo.result);
//
//                    if (jsonObject.getString("code").equals("1")) {
//                        dismiss();
//                        Util.showSnachBarDialog(mLeverDialogProgress, mContext.getResources().getColor(R.color.bg_green), "操作成功");
//
//                    } else {
//                        Util.showSnachBarDialog(mLeverDialogProgress, mContext.getResources().getColor(R.color.red), jsonObject.getString("info"));
//                    }
//
//                } catch (JSONException e) {
//
//
//                }
//
//            }
//
//            @Override
//            public void onFailure(HttpException error, String msg) {
//
//
//            }
//
//        });
    }


    public void setStatePost(String Is_cross) {


        Log.i("weifowhefihwefiuwhef", mMIs_cross);
        int num = (int) ((Math.random() * 9 + 1) * 100000);
        mJiazaiIsQuancang.setVisibility(View.VISIBLE);


        OkGo.<String>post(Config.SET_CROSS_STATE + "?rnd=" + String.valueOf(num)).params("position_id", mContract_id).params("is_cross", mMIs_cross).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {

                String result = response.body().toString();
                mLeverDialogFineTvSureLin.setClickable(true);
                mJiazai.setVisibility(View.GONE);
                Log.i("nchwetwekwueh", result);
                mJiazaiIsQuancang.setVisibility(View.GONE);

                try {
                    JSONObject jsonObject = new JSONObject(result);

                    if (jsonObject.getString("code").equals("1")) {

//                        dismiss();
                        Util.showSnachBarDialog(mLeverDialogProgress, mContext.getResources().getColor(R.color.bg_green), "操作成功");

                    } else {

                        Util.showSnachBarDialog(mLeverDialogProgress, mContext.getResources().getColor(R.color.red), jsonObject.getString("info"));
                    }

                } catch (JSONException e) {


                }
            }
        });


//        SharedPreferencesUtil sharedPreferences = SharedPreferencesUtil.getSharedPreferences(getContext());
//        PreferencesCookieStore cookieStore = CookieTool.getCookieStore(getContext(), sharedPreferences);
//        RequestParams params = new RequestParams();
//        params.addBodyParameter("position_id", mContract_id);
//        params.addBodyParameter("is_cross", mMIs_cross);
//
//        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.POST, Config.SET_CROSS_STATE + "?rnd=" + String.valueOf(num), params, new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//                mLeverDialogFineTvSureLin.setClickable(true);
//                mJiazai.setVisibility(View.GONE);
//                Log.i("nchwetwekwueh", responseInfo.result);
//                mJiazaiIsQuancang.setVisibility(View.GONE);
//
//                try {
//                    JSONObject jsonObject = new JSONObject(responseInfo.result);
//
//                    if (jsonObject.getString("code").equals("1")) {
//
////                        dismiss();
//                        Util.showSnachBarDialog(mLeverDialogProgress, mContext.getResources().getColor(R.color.bg_green), "操作成功");
//
//                    } else {
//
//                        Util.showSnachBarDialog(mLeverDialogProgress, mContext.getResources().getColor(R.color.red), jsonObject.getString("info"));
//                    }
//
//                } catch (JSONException e) {
//
//
//                }
//
//            }
//
//            @Override
//            public void onFailure(HttpException error, String msg) {
//
//
//            }
//
//        });
    }

    Handler handlers = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    getCosts();
                    break;

            }
        }
    };


    public void setState() {
        mDialogLeverZucang.setSelected(false);
        mDialogLeverQuancang.setSelected(false);

    }


}
